"""
通知设置相关数据模型
"""
from datetime import datetime, timezone
from typing import Optional
from enum import Enum
from pydantic import BaseModel, Field
from bson import ObjectId
from .base import PyObjectId

class NotificationSettings(BaseModel):
    """通知设置模型"""
    email_task_assigned: bool = Field(default=True, description="任务分配邮件通知")
    email_task_completed: bool = Field(default=True, description="任务完成邮件通知")
    email_project_update: bool = Field(default=False, description="项目更新邮件通知")
    email_deadline_reminder: bool = Field(default=True, description="截止日期邮件提醒")
    system_task_assigned: bool = Field(default=True, description="任务分配系统通知")
    system_task_completed: bool = Field(default=True, description="任务完成系统通知")
    system_project_update: bool = Field(default=True, description="项目更新系统通知")
    system_deadline_reminder: bool = Field(default=True, description="截止日期系统提醒")

class NotificationSettingsInDB(NotificationSettings):
    """数据库中的通知设置模型"""
    id: PyObjectId = Field(default_factory=PyObjectId, alias="_id")
    user_id: PyObjectId = Field(..., description="用户ID")
    created_at: datetime = Field(default_factory=datetime.utcnow, description="创建时间")
    updated_at: datetime = Field(default_factory=datetime.utcnow, description="更新时间")
    
    class Config:
        populate_by_name = True
        arbitrary_types_allowed = True
        json_encoders = {ObjectId: str}

class NotificationSettingsResponse(NotificationSettings):
    """通知设置响应模型"""
    id: str = Field(..., description="设置ID")
    user_id: str = Field(..., description="用户ID")
    created_at: datetime = Field(..., description="创建时间")
    updated_at: datetime = Field(..., description="更新时间")


# 通知记录相关模型

class NotificationType(str, Enum):
    """通知类型枚举"""
    # 项目相关
    PROJECT_CREATED = "project_created"  # 项目创建
    PROJECT_UPDATED = "project_updated"  # 项目更新
    PROJECT_DELETED = "project_deleted"  # 项目删除
    PROJECT_MEMBER_ADDED = "project_member_added"  # 项目成员添加
    # 需求相关
    REQUIREMENT_CREATED = "requirement_created"  # 需求创建
    REQUIREMENT_UPDATED = "requirement_updated"  # 需求更新
    REQUIREMENT_DELETED = "requirement_deleted"  # 需求删除
    # 任务相关
    TASK_CREATED = "task_created"  # 任务创建
    TASK_ASSIGNED = "task_assigned"  # 任务分配
    TASK_UPDATED = "task_updated"  # 任务更新
    TASK_COMPLETED = "task_completed"  # 任务完成
    TASK_STATUS_CHANGED = "task_status_changed"  # 任务状态变更
    TASK_DELETED = "task_deleted"  # 任务删除
    TASK_DEADLINE_REMINDER = "task_deadline_reminder"  # 任务截止日期提醒
    # 缺陷相关
    DEFECT_CREATED = "defect_created"  # 缺陷创建
    DEFECT_UPDATED = "defect_updated"  # 缺陷更新
    DEFECT_DELETED = "defect_deleted"  # 缺陷删除
    # 评论相关
    COMMENT_ADDED = "comment_added"  # 评论添加
    COMMENT_REPLIED = "comment_replied"  # 评论回复
    # 版本相关
    VERSION_CREATED = "version_created"  # 版本创建
    VERSION_UPDATED = "version_updated"  # 版本更新
    VERSION_DELETED = "version_deleted"  # 版本删除
    VERSION_RELEASED = "version_released"  # 版本发布
    VERSION_ROLLBACK = "version_rollback"  # 版本回滚
    VERSION_STATUS_CHANGED = "version_status_changed"  # 版本状态变更
    VERSION_TASK_ADDED = "version_task_added"  # 版本关联任务
    VERSION_DEFECT_ADDED = "version_defect_added"  # 版本关联缺陷
    VERSION_REQUIREMENT_ADDED = "version_requirement_added"  # 版本关联需求
    # 测试用例相关
    TEST_CASE_CREATED = "test_case_created"  # 测试用例创建
    TEST_CASE_UPDATED = "test_case_updated"  # 测试用例更新
    TEST_CASE_DELETED = "test_case_deleted"  # 测试用例删除
    TEST_CASE_ASSIGNED = "test_case_assigned"  # 测试用例分配
    TEST_CASE_STATUS_CHANGED = "test_case_status_changed"  # 测试用例状态变更
    TEST_CASE_EXECUTED = "test_case_executed"  # 测试用例执行
    # 文档/知识库相关
    DOCUMENT_CREATED = "document_created"  # 文档创建
    DOCUMENT_UPDATED = "document_updated"  # 文档更新
    DOCUMENT_DELETED = "document_deleted"  # 文档删除
    DOCUMENT_SHARED = "document_shared"  # 文档分享
    DOCUMENT_COMMENTED = "document_commented"  # 文档评论
    DOCUMENT_FAVORITED = "document_favorited"  # 文档收藏


class NotificationStatus(str, Enum):
    """通知状态枚举"""
    UNREAD = "unread"  # 未读
    READ = "read"  # 已读
    DELETED = "deleted"  # 已删除


class RelatedObjectType(str, Enum):
    """关联对象类型枚举"""
    TASK = "task"
    PROJECT = "project"
    REQUIREMENT = "requirement"
    VERSION = "version"
    COMMENT = "comment"
    DEFECT = "defect"
    TEST_CASE = "test_case"
    DOCUMENT = "document"


class Notification(BaseModel):
    """通知记录模型"""
    user_id: PyObjectId = Field(..., description="接收通知的用户ID")
    type: NotificationType = Field(..., description="通知类型")
    title: str = Field(..., min_length=1, max_length=200, description="通知标题")
    content: str = Field(..., min_length=1, max_length=2000, description="通知内容")
    related_type: Optional[RelatedObjectType] = Field(None, description="关联对象类型")
    related_id: Optional[PyObjectId] = Field(None, description="关联对象ID")
    is_read: bool = Field(default=False, description="是否已读")
    read_at: Optional[datetime] = Field(None, description="阅读时间")
    created_at: datetime = Field(default_factory=lambda: datetime.now(timezone.utc), description="创建时间")
    metadata: Optional[dict] = Field(default_factory=dict, description="额外元数据")


class NotificationInDB(Notification):
    """数据库中的通知模型"""
    id: PyObjectId = Field(default_factory=PyObjectId, alias="_id")
    
    class Config:
        populate_by_name = True
        arbitrary_types_allowed = True
        json_encoders = {ObjectId: str}


class NotificationResponse(BaseModel):
    """通知响应模型"""
    id: str = Field(..., description="通知ID")
    user_id: str = Field(..., description="接收通知的用户ID")
    type: str = Field(..., description="通知类型")
    title: str = Field(..., description="通知标题")
    content: str = Field(..., description="通知内容")
    related_type: Optional[str] = Field(None, description="关联对象类型")
    related_id: Optional[str] = Field(None, description="关联对象ID")
    is_read: bool = Field(..., description="是否已读")
    read_at: Optional[datetime] = Field(None, description="阅读时间")
    created_at: datetime = Field(..., description="创建时间")
    metadata: Optional[dict] = Field(default_factory=dict, description="额外元数据")
    
    class Config:
        json_encoders = {
            datetime: lambda v: v.isoformat() if v else None
        }
        json_schema_extra = {
            "example": {
                "id": "507f1f77bcf86cd799439011",
                "user_id": "507f1f77bcf86cd799439012",
                "type": "task_assigned",
                "title": "任务分配通知",
                "content": "您被分配了一个新任务",
                "related_type": "task",
                "related_id": "507f1f77bcf86cd799439013",
                "is_read": False,
                "read_at": None,
                "created_at": "2025-01-01T00:00:00+00:00",
                "metadata": {}
            }
        }


class NotificationCreate(BaseModel):
    """创建通知模型"""
    user_id: str = Field(..., description="接收通知的用户ID")
    type: NotificationType = Field(..., description="通知类型")
    title: str = Field(..., min_length=1, max_length=200, description="通知标题")
    content: str = Field(..., min_length=1, max_length=2000, description="通知内容")
    related_type: Optional[RelatedObjectType] = Field(None, description="关联对象类型")
    related_id: Optional[str] = Field(None, description="关联对象ID")
    metadata: Optional[dict] = Field(default_factory=dict, description="额外元数据")


class NotificationUpdate(BaseModel):
    """更新通知模型"""
    is_read: Optional[bool] = Field(None, description="是否已读")